package com.tuanbiao.ui.project;

import android.app.Activity;
import android.content.Intent;
import android.view.View;

import com.hzjq.data.entity.PmEntity;
import com.tuanbiao.R;
import com.tuanbiao.base.mvp.BasePresenterActivity;
import com.tuanbiao.constants.Constants;
import com.tuanbiao.controllers.ProjectController;
import com.tuanbiao.data.entity.ProjectCompanyType;
import com.tuanbiao.databinding.ActivityProjectAddBinding;
import com.tuanbiao.dialog.SelectDialog;
import com.tuanbiao.models.ProjectModel;
import com.tuanbiao.presenter.ProjectAddPresenter;
import com.tuanbiao.utils.ClickUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import timber.log.Timber;

/**
 * 新增或者编辑项目
 */
public class ProjectAddActivity extends BasePresenterActivity<ProjectAddPresenter, ActivityProjectAddBinding> implements ProjectController.AddView {

    private PmEntity pmEntity;
    private List<ProjectCompanyType> companyTypes;
    private ProjectCompanyType companyType;

    public static Intent createIntent(Activity activity) {
        return new Intent(activity, ProjectAddActivity.class);
    }

    @Override
    protected ProjectAddPresenter createPresenter() {
        return new ProjectAddPresenter(new ProjectModel(), this);
    }

    @Override
    protected ActivityProjectAddBinding getBindingView() {
        return ActivityProjectAddBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initView() {
        binding.ivToolbarBack.setOnClickListener(getBackListener());
        binding.tvCompanyTypeDesc.setOnClickListener(view -> showCompanyDialog());
        binding.btnCommit.setOnClickListener(view -> {
            checkData();
        });
    }

    private void checkData() {
        if (ClickUtil.isFastClick()) {
            return;
        }
        if (null == pmEntity) {
            pmEntity = new PmEntity();
        }
        pmEntity.setPmName(getString(binding.etProjectName));
        pmEntity.setPmHtId(getString(binding.etContractCode));
        pmEntity.setPmXmId(getString(binding.etProjectCode));
        pmEntity.setPmCompanyCode(getString(binding.etCompanyCode));
        pmEntity.setPmManagerCode(getString(binding.etIdCard));
        if (null == companyType) {
            showMessage("请选择公司性质");
            return;
        }
        pmEntity.setNatureBusiness(companyType.isBusiness());
        mPresenter.commit(pmEntity);
    }

    private void showCompanyDialog() {
        if (null == companyTypes) {
            companyTypes = new ArrayList<>();
            companyTypes.add(new ProjectCompanyType(Constants.CompanyType.BUSINESS));
            companyTypes.add(new ProjectCompanyType(Constants.CompanyType.NO_BUSINESS));
        }
        boolean isBusiness = null != pmEntity && pmEntity.isNatureBusiness();
        // 单选对话框
        new SelectDialog.Builder(this)
                .setTitle("请选择公司性质")
                .setList(companyTypes)
                // 设置单选模式
                .setSingleSelect()
                // 设置默认选中
                .setSelect(isBusiness ? 0 : 1)
                .setListener((SelectDialog.OnListener<ProjectCompanyType>) (dialog, data) -> {
                    Set<Map.Entry<Integer, ProjectCompanyType>> entries = data.entrySet();
                    for (Map.Entry<Integer, ProjectCompanyType> entry : entries) {
                        companyType = entry.getValue();
                    }
                    checkBusiness(companyType);
                    if (pmEntity != null) {
                        pmEntity.setNatureBusiness(companyType.isBusiness());
                    }
                    binding.tvCompanyTypeDesc.setText(companyType.itemName());
                })
                .show();
    }

    private void checkBusiness(ProjectCompanyType companyType) {
        if (null != companyType) {
            binding.llContractCode.setVisibility(companyType.isBusiness()? View.VISIBLE:View.GONE);
            binding.vContractCode.setVisibility(companyType.isBusiness()? View.VISIBLE:View.GONE);
            binding.llProjectCode.setVisibility(companyType.isBusiness()? View.VISIBLE:View.GONE);
            binding.vProjectCode.setVisibility(companyType.isBusiness()? View.VISIBLE:View.GONE);
        }
    }

    @Override
    public void showDetails(PmEntity entity) {
        binding.tvToolbarTitle.setText(R.string.project_change_title);
        pmEntity = entity;
        companyType = new ProjectCompanyType(entity.isNatureBusiness() ? Constants.CompanyType.BUSINESS : Constants.CompanyType.NO_BUSINESS);
        checkBusiness(companyType);
        binding.etProjectName.setText(entity.getPmName());
        binding.etContractCode.setText(entity.getPmHtId());
        binding.etContractCode.setEnabled(false);
        binding.llContractCode.setBackgroundColor(getCompatColor(R.color.gray20));
        binding.etProjectCode.setText(entity.getPmXmId());
        binding.etCompanyCode.setText(entity.getPmCompanyCode());
        binding.etIdCard.setText(entity.getPmManagerCode());
        binding.tvCompanyTypeDesc.setText(entity.isNatureBusiness() ? Constants.CompanyType.BUSINESS : Constants.CompanyType.NO_BUSINESS);
    }

    @Override
    public void showDetails(String companyCode, boolean isBusiness) {
        Timber.i("showDetails:" + companyCode + "," + isBusiness);
        if (null == pmEntity) {
            pmEntity = new PmEntity();
        }
        companyType = new ProjectCompanyType(isBusiness ? Constants.CompanyType.BUSINESS : Constants.CompanyType.NO_BUSINESS);
        checkBusiness(companyType);
        pmEntity.setPmCompanyCode(companyCode);
        pmEntity.setNatureBusiness(isBusiness);
        binding.etCompanyCode.setText(companyCode);
        binding.tvCompanyTypeDesc.setText(isBusiness ? Constants.CompanyType.BUSINESS : Constants.CompanyType.NO_BUSINESS);
    }

    @Override
    public void onResultSuccess() {
        setResult(RESULT_OK);
        killMyself();
    }
}
